package com.yy.controller.systemmanager;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yy.common.config.RCConfig;
import com.yy.common.core.AjaxResult;
import com.yy.common.core.HttpStatus;
import com.yy.common.core.page.TableDataInfo;
import com.yy.common.utils.DateUtils;
import com.yy.common.utils.JwtHelper;
import com.yy.common.utils.MD5Util;
import com.yy.common.utils.file.FileUploadUtils;
import com.yy.common.utils.file.MimeTypeUtils;
import com.yy.pojo.User;
import com.yy.service.UserService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 * ClassName: AccountController
 * Package: com.yy.controller.systemmanager
 * Description:
 *
 * @Author yy
 * @Create 2023-12-21 18:08
 * @Version 1.0
 */
@RequestMapping("/system-manager/account-manage")
@RestController
public class AccountController {

    @Autowired
    private UserService userService;

    @Autowired
    private JwtHelper jwtHelper;

    @GetMapping("query")
    public TableDataInfo queryAllSatisfiedUsers(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                                @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                User user) {
        IPage<User> page = userService.getUsersList(pageNum, pageSize, user);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(page.getRecords());
        rspData.setTotal(page.getTotal());
        return rspData;
    }

    @GetMapping("{id}")
    public AjaxResult queryDetailUser(@PathVariable("id") Long userId) {
        return AjaxResult.success(userService.getById(userId));
    }


    @PostMapping("add")
    public AjaxResult addUser(@RequestPart(value = "avatar", required = false) MultipartFile avatarFile,
                              @RequestPart("user") User user) throws Exception {
        if (ObjectUtils.isNotEmpty(avatarFile) && !avatarFile.isEmpty()) {
            String avatar = FileUploadUtils.upload(RCConfig.getAvatarPath(), avatarFile, MimeTypeUtils.IMAGE_EXTENSION);
            user.setAvatar(avatar);
        }

        boolean save = userService.save(user);
        user.setPassword(MD5Util.encrypt(user.getPassword()));
        if (save) {
            return AjaxResult.success();
        }
        return AjaxResult.error("新增失败");

    }

    @PutMapping("update")
    public AjaxResult update(@RequestPart(value = "avatar", required = false) MultipartFile avatarFile,
                             @RequestPart("user") User user,
                             @RequestHeader(value = "token", required = false) String token) throws Exception {

        if (ObjectUtils.isNotEmpty(avatarFile) && !avatarFile.isEmpty()) {
            // 更新头像
            String avatar = FileUploadUtils.upload(RCConfig.getAvatarPath(), avatarFile, MimeTypeUtils.IMAGE_EXTENSION);
            user.setAvatar(avatar);
        }
        if (StringUtils.isNotEmpty(user.getPassword())) {
            user.setPassword(MD5Util.encrypt(user.getPassword()));
        }
//        user.setUpdateBy(String.valueOf(jwtHelper.getUserId(token)));
        user.setUpdateTime(DateUtils.getNowDate());
        boolean update = userService.updateById(user);
        if (update) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @DeleteMapping("{ids}")
    public AjaxResult delete(@PathVariable List<Long> ids) {
        boolean remove = userService.removeByIds(ids);
        if (remove) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
}
